package Controller;

import DAOs.exceptions.NonexistentEntityException;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.*;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;

/**
 *
 * @author USUARIO
 */
public class ReportController {

    public ReportController() throws DocumentException, FileNotFoundException, IOException {
        Document document = new Document();
        PdfWriter.getInstance(document, new FileOutputStream("Reportes\\hello.pdf"));
        document.open();
        document.add(new Paragraph("Hello asdf"));
        document.close();

        /*try {
         //File path = new File ("c:\\Hello.pdf");
         //Desktop.getDesktop().open(path);
         } catch (IOException ioe) {
         System.out.println (ioe);
         }*/
    }

    public void printer() throws IOException {

        FileInputStream fis = null;
        try {
            fis = new FileInputStream("C:\\Hello.pdf");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        if (fis == null) {
            System.err.println("No existe el archivo");
            return;
        }

        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        DocAttributeSet das = new HashDocAttributeSet();
        Doc doc = new SimpleDoc(fis, flavor, null);

        //PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();

        PrintService service = PrintServiceLookup.lookupDefaultPrintService();

        if (service != null) {
            System.out.println("Si existe el archivo");
            DocPrintJob job = service.createPrintJob();
            try {
                System.out.println("Intenta imprimir");
                job.print(doc, null);

            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            System.err.println("No existen impresoras instaladas");
        }

        fis.close();
        System.out.println("cerró");
    }

    public static void main(String[] args) throws NonexistentEntityException, IOException {
        try {
            ReportController rc = new ReportController();
            rc.printer();
        } catch (DocumentException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ReportController.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
